import type { App } from "vue";
import type { Type } from "@/components/form-designer/form-widget/field-widget";
export const setId = ()=>{
    let str = '';
    for(let i = 0;i < 32; i++){
        str += Math.floor(Math.random() * 16).toString(16)
        if(i === 7 || i === 11 || i === 15 || i === 19){
            str += '-'
        }
    }
    return str
}

export const getDefaultFormConfig = ()=> {
    return {
        modelName: 'formData',
        refName: 'vForm',
        rulesName: 'rules',
        labelWidth: 80,
        labelPosition: 'left',
        size: 'default',
        labelAlign: 'label-left',
        cssCode: '',
        customClass: [],
        functions: '',  //全局函数
        layoutType: 'PC',
        jsonVersion: 3,
        onFormCreated: '',
        onFormMounted: '',
        onFormDataChange: '',
    }
}

export const overwrite = (target:any, source:any) => {
    const keys = Reflect.ownKeys(source)
    keys.forEach((key:any)=>{
        target[key] = source[key]
    })
}

export const downFile = (response:any, fileName:string, fileExtendName:string)=>{
    var type = ''
    switch (fileExtendName) {
        case 'jpg':
            type = 'image/jpeg'
            break
        case 'doc':
            type = 'application/msword'
            break
        case 'pdf':
            type = 'application/pdf'
            break
        case 'xls':
            type = 'application/vnd.ms-excel'
            break
        case 'xlsx':
            type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            break
        case 'ppt':
            type = 'application/vnd.ms-powerpoint'
            break
        case 'zip':
            type = 'application/zip'
            break
        case 'gif':
            type = 'image/gif'
            break
        case 'jpeg':
            type = 'image/jpeg'
            break
        case 'png':
            type = 'image/png'
            break
        case 'css':
            type = 'text/css'
            break
        case 'xsl':
            type = 'text/xml'
            break
        case 'mpeg':
            type = 'video/mpe'
            break
        case 'avi':
            type = 'video/x-msvideo'
            break
        case 'txt':
            type = 'text/plain'
            break
        case 'html':
            type = 'text/htm'
            break
        case 'docx':
            type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
            break
        case 'json':
            type = 'application/json'
            break
        default:
            type = 'text/plain'
    }
    const blob = new Blob([response], {type: type})
    const link = document.createElement('a')
    link.download = fileName
    link.style.display = 'none'
    const url = URL.createObjectURL(blob)
    link.href = url
    document.body.appendChild(link)
    link.click()
    URL.revokeObjectURL(url)
    document.body.removeChild(link)
}


export const registerComp =(app:App,components:Type)=>{
    for (const [key, component] of Object.entries(components)) {
        app.component(key, component)
    }
}